<?php

namespace app\admin\controller\yungift;

use app\common\controller\Backend;
use \addons\yungift\model\Config as ConfigModel;

/**
 * 配置模块
 */
class Config extends Backend
{
    /**
     * @var ConfigModel
     */
    protected $model;

    protected $noNeedRight = ['groupList'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ConfigModel();
    }

    /**
     * 基础设置
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if (isset($params[ConfigModel::GROUP_BASE]['login_methods'])) {
                    $params[ConfigModel::GROUP_BASE]['login_methods'] =
                        implode(',', $params[ConfigModel::GROUP_BASE]['login_methods']);
                }
                if (isset($params[ConfigModel::GROUP_BASE]['disable_options'])) {
                    $params[ConfigModel::GROUP_BASE]['disable_options'] =
                        implode(',', $params[ConfigModel::GROUP_BASE]['disable_options']);
                }
                if (isset($params[ConfigModel::GROUP_SMS]['exchange_notice_scenes'])) {
                    $params[ConfigModel::GROUP_SMS]['exchange_notice_scenes'] =
                        implode(',', $params[ConfigModel::GROUP_SMS]['exchange_notice_scenes']);
                }
                if (isset($params[ConfigModel::GROUP_SMS]['deploy_notice_scenes'])) {
                    $params[ConfigModel::GROUP_SMS]['deploy_notice_scenes'] =
                        implode(',', $params[ConfigModel::GROUP_SMS]['deploy_notice_scenes']);
                }
                ConfigModel::saveAllAttributes($params);
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $row = ConfigModel::getAllAttributes();
        $this->view->assign('row', $row);

        $this->view->assign('loginMethods', [
            'name_code'     => ['name' => '姓名登录码', 'tip' => '姓名+登录码方式，登录码需要提前生成好'],
            'number_code'   => ['name' => '编号登录码', 'tip' => '编号+登录码方式，登录码需要提前生成好'],
            'phone_captcha' => ['name' => '手机验证码', 'tip' => '手机+验证码方式，手机号必须唯一，必须配置阿里云短信'],
            'wechat_phone'  => ['name' => '微信快捷验证', 'tip' => '手机+验证码方式，手机号必须唯一，可以不用配置阿里云短信']
        ]);
        $this->view->assign('codeMethods',
            ['number_suffix' => '编号后6位', 'phone_suffix' => '手机号后6位', 'string_rand' => '随机8位数字英文']);
        $this->view->assign('yesOrNo', [1 => '是', 0 => '否']);
        $this->view->assign('disableOptions', [
            'user_name'    => ['name' => '姓名', 'tip' => ''],
            'user_number'  => ['name' => '编号', 'tip' => ''],
            'user_phone'   => ['name' => '手机号', 'tip' => ''],
            'user_group'   => ['name' => '所在分组', 'tip' => ''],
            'change_phone' => ['name' => '修改手机号', 'tip' => ''],
            'change_code'  => ['name' => '修改登录码', 'tip' => ''],
            'feedback'     => ['name' => '意见反馈', 'tip' => ''],
            'logout'       => ['name' => '退出登录', 'tip' => ''],
        ]);
        $this->view->assign('exchangeNoticeScenes', [1 => '实物兑换', 2 => '虚拟商品兑换']);
        $this->view->assign('deployNoticeScenes', [1 => '线下领取', 2 => '快递邮寄']);

        return $this->view->fetch();
    }
}
